package tx.yt.charge.web.admin;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import tx.yt.charge.model.User;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: chenmiao@hupu.com
 * Date: 2015-1-8
 */
@Controller
@RequestMapping(value = "/admin")
public class UserController {

    private final static Map<String,User> defaultUsers = new HashMap();
    private int userSize = 0 ;

    public UserController(){
        defaultUsers.put("1",new User(1,"张三","abcde"));
        defaultUsers.put("2",new User(2,"陈帅","xxand"));
        defaultUsers.put("3",new User(3,"李斯","qqpps"));
        defaultUsers.put("4",new User(4,"刘昊","wqcde"));
        userSize = defaultUsers.size();
    }

    @RequestMapping(value = "/userAdd",method = RequestMethod.GET)
    public String toUserAdd(){
        return "admin/user/add";
    }

    @RequestMapping(value = "/userAdd",method = RequestMethod.POST)
    public String addUser(User user){
        defaultUsers.put(String.valueOf(++userSize),user);
        return "redirect:/admin/userList";
    }


    @RequestMapping(value = "/userList")
    public String listUser(Model model){
        model.addAttribute("users", defaultUsers);
        System.out.println(defaultUsers.size());
        return "admin/user/list";
    }
}
